Functions, loops and conditional statements

ANU BDSI
workshop
Introduction to R programming

Emi Tanaka

Biological Data Science Institute

3rd April 2024

Current learning objective

  • -Conduct elementary arithmetic operations using R
  • -Navigate the RStudio interactive development environment (IDE)
  • -Compute basic summary statistics including mean, median, quartiles, and standard deviation using R
  • -Install external packages in R to extend functionality
  • -Comprehend various object types in R
  • -Manipulate lists, matrices, and vectors in R
  • -Grasp the concept of missing values within the R environment
  • -Import and export data in R
  • Create basic functions, employ conditional statements, and utilize for loops in R
  • Decipher error messages and do basic troubleshooting

Functions

  • A function in R has three parts:
    • formals(): the list of input arguments,
    • body(): the code inside the function,
    • environment(): the environment (out of scope for this workshop).
  • Writing your own functions can make repetitive tasks easier.
  • You can make a new function using function().

Examining functions

  • Writing the name of the function without arguments shows the internal code:

Loops

Conditional statements

  • The condition in the if statement needs to evaluate to a single TRUE or FALSE
  • ifelse() is the vectorised

Example

Basic troubleshooting

If you encounter an error,

  1. Read the error message!
  2. Google the error message
  3. Ask for help with a reproducible example

🆘 Asking for help

  • What do you think about the question below?

🆘 Asking for help 1 Part 2

  • What do you think now?

I am looking to adjust the size of two separate ggplots within the same R chunk in Rmarkdown. These plots must be different when outputted as a pdf, so defining the dimensions at the beginning of the chunk doesn’t work. Does anyone have any ideas? My code is below.

```{r, fig.height = 3, fig.width = 3}
ggplot(df, aes(weight, height)) +
  geom_point()

ggplot(df, aes(height, volume)) +
  geom_point()
```

🆘 Asking for help 1 Part 3

  • Is this better?

I am looking to adjust the size of two separate ggplots within the same R chunk in Rmarkdown. These plots must be different when outputted as a pdf, so defining the dimensions at the beginning of the chunk doesn’t work. Does anyone have any ideas? My code is below.

```{r, fig.height = 3, fig.width = 3}
library(ggplot2)
ggplot(df, aes(weight, height)) +
  geom_point()

ggplot(df, aes(height, volume)) +
  geom_point()
```

🆘 Asking for help 1 Part 4

  • Okay better now?

I am looking to adjust the size of two separate ggplots within the same R chunk in Rmarkdown. These plots must be different when outputted as a pdf, so defining the dimensions at the beginning of the chunk doesn’t work. Does anyone have any ideas? My code is below.

```{r, fig.height = 3, fig.width = 3}
library(ggplot2)
df <- read.csv("mydata.csv")
ggplot(df, aes(weight, height)) +
  geom_point()

ggplot(df, aes(height, volume)) +
  geom_point()
```

🆘 Asking for help 1 Part 5

  • Are we done now?

I am looking to adjust the size of two separate ggplots within the same R chunk in Rmarkdown. These plots must be different when outputted as a pdf, so defining the dimensions at the beginning of the chunk doesn’t work. Does anyone have any ideas? My code is below.

```{r, fig.height = 3, fig.width = 3}
library(ggplot2)
ggplot(trees, aes(Girth, Height)) +
  geom_point()

ggplot(trees, aes(Height, Volume)) +
  geom_point()
```

❓ How to ask questions?

Checklist (note: not an exhaustive checklist)

If the question is asked in an public forum or similar:

If the problem is computer system related

If the problem is based on data

🆘 Asking for help 1 Check

🆘 Asking for help 2

  • How about the question on the right?
  • What makes it hard or easy for people to answer this question?

Session Information

You can easily get the session information in R using sessionInfo().
Scroll to see the packages used to make these slides.

R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.2.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Australia/Sydney
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4    
 [5] purrr_1.0.2     readr_2.1.4     tidyr_1.3.0     tibble_3.2.1   
 [9] ggplot2_3.5.0   tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] gtable_0.3.4      jsonlite_1.8.8    compiler_4.3.1    tidyselect_1.2.0 
 [5] scales_1.3.0      yaml_2.3.8        fastmap_1.1.1     R6_2.5.1         
 [9] generics_0.1.3    knitr_1.45        munsell_0.5.0     pillar_1.9.0     
[13] tzdb_0.4.0        rlang_1.1.3       utf8_1.2.4        stringi_1.8.3    
[17] xfun_0.41         timechange_0.2.0  cli_3.6.2         withr_3.0.0      
[21] magrittr_2.0.3    digest_0.6.34     grid_4.3.1        rstudioapi_0.15.0
[25] hms_1.1.3         lifecycle_1.0.4   vctrs_0.6.5       evaluate_0.23    
[29] glue_1.7.0        fansi_1.0.6       colorspace_2.1-0  rmarkdown_2.25   
[33] tools_4.3.1       pkgconfig_2.0.3   htmltools_0.5.7  

🎁 Reproducible Example with reprex LIVE DEMO

  • Copy your minimum reproducible example then run